﻿using AutoMapper;
using MediatR;
using T.S.BaseData.Api.AppClinet.Commend.Goods;
using T.S.BaseData.Api.Dto.Goods;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Goods
{
    public class BasicinfoGetCommandHandle : IRequestHandler<BasicinfoGetCommand, ApiPaging<GetBasicinfoDto>>
    {
        private readonly IBaseRepository<BasicInfo> basecRepo;
        private readonly IBaseRepository<GoodsInfo> goodsRepo;
        private readonly IMapper mapper;

        public BasicinfoGetCommandHandle(IBaseRepository<BasicInfo> basecRepo, 
            IBaseRepository<GoodsInfo> goodsRepo, 
            IMapper mapper)
        {
            this.basecRepo = basecRepo;
            this.goodsRepo = goodsRepo;
            this.mapper = mapper;
        }

        public Task<ApiPaging<GetBasicinfoDto>> Handle(BasicinfoGetCommand request, CancellationToken cancellationToken)
        {

            ApiPaging<GetBasicinfoDto> result = new ApiPaging<GetBasicinfoDto>();

            var basec = basecRepo.GetAll();
            var goods = goodsRepo.GetAll();

            if (request.Id>0)
            {
                basec = basec.Where(x=>x.Id == request.Id);
            }

            var list = from b in basec
                       join g in goods on b.GoodsId equals g.Id
                       select new GetBasicinfoDto 
                       {
                           Id = b.Id,
                           GoodsId = g.Id,
                           GoodsName = g.GoodsName,
                           GoodsBatch = b.GoodsBatch,
                           GoodsExpiration = b.GoodsExpiration,
                           GoodsLicense = b.GoodsLicense,
                           GoodsEnterprise = b.GoodsEnterprise,
                           GoodsPhoto = b.GoodsPhoto,
                           CreatePeop = b.CreatePeop,
                           CreateTime = b.CreateTime,
                           IsDelete = b.IsDelete,
                       };
            var model = mapper.Map<List<GetBasicinfoDto>>(list);

            result.Iteims = model;

            return Task.FromResult(result);


        }
    }
}
